﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Biz;

namespace MockPrjTeam4.Pages.Views
{
    public partial class PopupContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (chkInactive.Checked)
                {
                    gvListContact.DataSource = ContactBiz.GetListContact();
                    gvListContact.DataBind();
                }
                else
                {
                    gvListContact.DataSource = ContactBiz.GetListActiveContacts();
                    gvListContact.DataBind();
                }
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            int id = (int)gvListContact.SelectedDataKey.Values["ContactId"];
            Contacts contact = ContactBiz.GetContactById(id);
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.AppendFormat("window.opener.document.getElementById('txtContactName').value  = '{0}';",
                contact.FirstName + " " + contact.SurName);
            script.AppendFormat("window.opener.document.getElementById('hdfContact').value  = '{0}';",
                contact.ContactID);
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtFirstname.Text = "";
            txtSurname.Text = "";
        }

        protected void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInactive.Checked)
            {
                gvListContact.DataSource = ContactBiz.GetListContact();
                gvListContact.DataBind();
            }
            else
            {
                gvListContact.DataSource = ContactBiz.GetListActiveContacts();
                gvListContact.DataBind();
            }
        }

        protected void btnNone_Click(object sender, EventArgs e)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");

            script.AppendFormat("window.opener.document.getElementById('txtContactName').value  = '{0}';",
                "");
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IList<Contacts> contact;
            if (chkInactive.Checked)
            {
                contact = ContactBiz.Search(txtFirstname.Text, txtSurname.Text);
            }
            else
            {
                contact = ContactBiz.Search(txtFirstname.Text, txtSurname.Text, "true");
            }
            gvListContact.DataSource = contact;
            gvListContact.DataBind();
        }

        protected void gvListContact_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListContact.PageIndex = e.NewPageIndex;
            if (chkInactive.Checked)
            {
                gvListContact.DataSource = ContactBiz.GetListContact();
                gvListContact.DataBind();
            }
            else
            {
                gvListContact.DataSource = ContactBiz.GetListActiveContacts();
                gvListContact.DataBind();
            }
        }
    }
}